<?php
declare (strict_types = 1);

namespace app\api\controller;
use app\api\controller\Base;
use app\common\model\Advert;
use app\common\model\Infinite;
use app\common\model\ProductCate;
use app\common\model\Product as ProductModel;
use app\common\model\ProductNorms;
use app\common\model\ProductNormsValue;
use app\common\model\Danye;

use \think\Request;

class Product extends Base
{
	/**
	 * 商户商品列表
	 */
	public function index(Request $request){
		$title = $request->param('title/s');
        $cate_id = $request->param('cate_id/d');
        $sort = $request->param('sort/d','1');
        /*商品分类*/
        $w = [];
        $w[] = ['status','=','1'];
        $w[] = ['pid','=','0'];
        $cate = ProductCate::getAllList($w,'id,title,imgurl',"sort desc,id desc");
        foreach($cate as $k=>&$v){
            $v['imgurl'] = imageUrl($v['imgurl']);
        }
        //商品列表
        $whe = [];
        if(!empty($title)){
            $whe[] = ['title','like','%'.$title.'%'];
        }
        if(!empty($cate_id)){
            $whe[] = ['cate_id','=',$cate_id];
        } 
        $whe[] = ['status','=','1'];
        //排序 1综合倒叙 2正序 3 销量倒序 4销量正序 5价格倒序 6价格正序
		if($sort==2){
			$order = "sort desc,id desc";
		}elseif($sort==1){
			$order = "sort asc,id asc";
		}elseif($sort==4){
			$order = "sales desc,id desc";
		}elseif($sort==3){
			$order = "sales asc,id desc";
		}elseif($sort==6){
			$order = "price desc,id desc";
		}elseif($sort==5){
			$order = "price asc,id asc";
		}
        
        // dd($whe);
        //查询商品
        $product = ProductModel::getList($whe,'id,title,imgurl,price,sales',$order,$this->page);
        foreach($product['list'] as $kk=>&$vv){
            $vv['imgurl'] = imageUrl($vv['imgurl']);
            $vv['stone'] = $vv['price'] * 10;
        }
		// $cate = array_chunk($cate, 10);
		$whe_advert = [];
		$whe_advert[] = ['type','=','3'];
		$advert = Advert::getAllList($whe_advert,'id,imgurl,type,ttype,url,title','sort asc,id desc');
		foreach ($advert as $key => &$val) {
			$val['imgurl'] = imageUrl($val['imgurl']);
		}
        return $this->renderSuccess("请求成功",compact('advert','cate','product'));
	}

	/**
	 * 商品详情
	 */
	public function detail(Request $request){
		$product_id = $request->param('product_id/d');
		$ww = [];
		$ww[] = ['id','=',$product_id];
		$field = "id,title,imgurl,imgurls,price,content,norms_type,stock,status,yunfei";
		$product = ProductModel::getInfo($ww,$field);
		if($product['status'] != '1'){
			return $this->renderError("该商品已下架或删除");
		}
		$product['imgurl'] = imageUrl($product['imgurl']);
		$product['stone'] = jisuan($product['price'],'10','*','0');
		$imgurls = explode(',',$product['imgurls']);
		foreach($imgurls as $k=>$v){
			$imgurls[$k] = imageUrl($v);
		}
		$product['imgurls'] = $imgurls;
		$product['content'] = contentUrl($product['content']);
		$norms = ProductNorms::getAllList(['product_id'=>$product['id']],'norms_name,norms_values');
    	foreach($norms as $k=>&$v){
    		$norms_values = explode(',',$v['norms_values']);
    		$res = [];
    		foreach($norms_values as $kk=>$vv){
    			$res[$kk]['name'] = $vv;
    		}
    		$v['norms_values'] = $res;
    	}
    	$product['sku'] = $norms;
    	return $this->renderSuccess("请求成功",compact('product'));
	}
	
	/**
	 * 获取规格对应的属性值
	 */
	public function norms(Request $request){
		$product_id = $request->param("product_id");
		$sku = $request->param('sku');
		if (empty($product_id)) {
		    return $this->renderError("缺少参数");
		}
		if (empty($sku)){
 			return $this->renderError("请选择规格SKU");
		}
		$sku_arr = explode(',',trim($sku,','));
		$sku_count = count($sku_arr);
		$norms_count = ProductNorms::where(['product_id'=>$product_id])->count();
		if($sku_count != $norms_count){
			return $this->renderError("请选择规格");
		}
		sort($sku_arr, SORT_STRING);
		$sku = implode(',', $sku_arr);
		$data = ProductNormsValue::where('product_id',$product_id)->where('sku',$sku)->field('id,stock,price,imgurl,sku')->find();
		$data['imgurl'] = imageUrl($data['imgurl']);		
		$data['stone'] = jisuan($data['price'],'10','*','0');
		return $this->renderSuccess("请求成功",$data);
	}

    /**
     * 分类
     * @param Request $request
     * @return mixed
     */
    public function cate(Request $request)
    {
        $where = [];
        $where[] = ['status', '=', '1'];
        $cate = ProductCate::getAllList($where);
        foreach ($cate as $k => &$item) {
            $ww = [];
            $ww[] = ['cate_id', '=', $item['id']];
            $ww[] = ['status', '=', 1];
            $field = 'id, title, imgurl';
            $product = ProductModel::getList($ww, $field, 'id desc', 30);
            $item['products']= $product;
        }
        return $this->renderSuccess("请求成功",$cate);
    }
    
    /**
     * 分类
     * @param Request $request
     * @return mixed
     */
    public function product_list(Request $request)
    {
        $cate_id = $request->param("cate_id");
		if (empty($cate_id)) {
		    return $this->renderError("缺少参数");
		}
        $ww = [];
        $ww[] = ['cate_id', '=', $cate_id];
        $ww[] = ['status', '=', 1];
        $field = 'id, title, imgurl';
        $product = ProductModel::getList($ww, $field, 'id desc', 30);
        foreach ($product['list'] as &$val){
            $val['imgurl'] = imageUrl($val['imgurl']);
        }
        return $this->renderSuccess("请求成功",$product);
    }
}